import 'package:cunyutech_flutter_ui/src/theme/main.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter/widgets.dart';

/// 输入框
BoxDecoration textInputDecoration = BoxDecoration(
  color: Color(0x33000000),
  borderRadius: BorderRadius.circular(4),
);

class CYTextInput extends StatefulWidget {
  final String value;
  final ValueChanged<String> onChange;
  final ValueChanged<String> onSubmited;
  final EdgeInsetsGeometry margin;
  final EdgeInsetsGeometry padding;
  final double height;
  final double width;
  final String placeholder;
  final TextStyle placeholderStyle;
  final String errorText;
  final TextInputType keyboardType;
  final TextInputAction keyboardAction;
  final Widget leading;
  final Widget action;
  final int maxLength;
  final bool maxLengthEnforced;
  final bool obscureText;
  final int minLines;
  final int maxLines;
  final Decoration decoration;
  final TextAlign textAlign;
  final TextEditingController controller;
  final RegExp rule;
  final bool autofocus;
  final AlignmentGeometry alignment;
  final FocusNode focusNode;
  final TextStyle textStyle;

  CYTextInput({
    Key key,
    this.value,
    this.onChange,
    this.onSubmited,
    this.margin,
    this.height,
    this.width,
    this.placeholder,
    this.placeholderStyle,
    this.errorText,
    this.keyboardType,
    this.keyboardAction,
    this.leading,
    this.action,
    this.maxLength,
    this.maxLengthEnforced,
    this.obscureText = false,
    this.minLines,
    this.maxLines,
    this.decoration,
    this.padding,
    this.textAlign,
    this.controller,
    this.rule,
    this.autofocus = false,
    this.alignment,
    this.focusNode,
    this.textStyle,
  }) : super(key: key);

  _CYTextInputState createState() => _CYTextInputState();
}

class _CYTextInputState extends State<CYTextInput> {
  TextEditingController controller;

  @override
  void initState() {
    super.initState();
    controller = TextEditingController(text: widget.value);
  }

  @override
  Widget build(BuildContext context) {
    var cs = CYThemeData.of(context);
    return Container(
        height: widget.height,
        width: widget.width,
        margin: widget.margin,
        decoration: widget.decoration ?? textInputDecoration,
        padding: widget.padding ?? EdgeInsets.symmetric(horizontal: CYMargin.Default, vertical: 0),
        alignment: widget.alignment ?? Alignment.topCenter,
        child: Row(
          crossAxisAlignment: CrossAxisAlignment.center,
          children: <Widget>[
            widget.leading,
            Expanded(
              child: CupertinoTextField(
                textAlignVertical: TextAlignVertical.center,
                textAlign: widget.textAlign ?? TextAlign.start,
                minLines: widget.minLines,
                maxLines: widget.maxLines,
                keyboardType: widget.keyboardType ?? TextInputType.text,
                textInputAction: widget.keyboardAction ?? TextInputAction.done,
                controller: widget.controller ?? controller,
                onChanged: widget.onChange,
                onSubmitted: widget.onSubmited,
                keyboardAppearance: Brightness.dark,
                autofocus: widget.autofocus,
                style: widget.textStyle ?? TextStyle(color: cs.primaryForeColor, fontSize: CYFontSize.Large, height: 1.2),
                maxLengthEnforced: widget.maxLengthEnforced ?? false,
                obscureText: widget.obscureText,
                inputFormatters: <TextInputFormatter>[
                  widget.rule != null ? WhitelistingTextInputFormatter(widget.rule) : null,
                  LengthLimitingTextInputFormatter(widget.maxLength),
                ].where((item)=> item!=null).toList(),
                placeholder: widget.placeholder ??  "请输入",
                placeholderStyle: widget.placeholderStyle ?? TextStyle(color: cs.foreColorSystem.placeholderColor, fontSize: CYFontSize.Large ),
                decoration: BoxDecoration(),
                focusNode: widget.focusNode,
              ),
            ),
            widget.action,
          ].where((item)=> item != null).toList(),
        ),
    );
  }
}
